/**
 * GeoSOA/Spatialytics/widgets/ThematicStyleChooser.js
 *
 * Thematic style chooser
 *
 * Copyright (C) 2008-2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090813
 **/


dojo.provide("GeoSOA.Spatialytics.widgets.ThematicStyleChooser");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("dijit.TitlePane");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dojo.data.ItemFileWriteStore");

dojo.declare("GeoSOA.Spatialytics.widgets.ThematicStyleChooser",
		[dijit._Widget, dijit._Templated], {

	// title: String
	//	the title label
	title: "Thematic style",
	
	// --- private attributes:
	_solapContext: null,
	_mapStyles: null,
	_mapStyleCounter: 0,
	_currentId: 0,
	// ---
	
	templatePath: dojo.moduleUrl("GeoSOA.Spatialytics.widgets","templates/ThematicStyleChooser.html"),
	widgetsInTemplate: true,
	
	constructor: function () {
		this._mapStyles = [];
	},
	
	// -- postCreate function (happens after rendering of widget but
	// before creation of sub-widgets ??)
	postCreate: function () {
		var that = this;
		var itemsJson = {
			identifier: 'id',
			label: 'label',
			items: []};
		var dataStore = new dojo.data.ItemFileWriteStore({ data: itemsJson });
		this.filteringSelectWidget.attr('searchAttr', 'label');
		this.filteringSelectWidget.attr('store', dataStore);
		this.filteringSelectWidget.attr('onChange',
				function() {
					that._handleChange();
				}
		);
		this.inherited(arguments);
	},

	// --- private methods
	_handleChange: function () {
		this._updateCurrentMapStyle();
		if (this._solapContext != null) {
			this._solapContext.setMapStyle(this.getCurrentMapStyle());
			
		}
	},
	
	_updateCurrentMapStyle: function () {
		var item = this._getCurrentItemAttr();
		if (item != null) {
			this._currentId = this.filteringSelectWidget.attr('item').id;
			return this._mapStyles[this._currentId];
		}
	},
	
	// --- getters/setters
	
	_setTitleAttr: function (/*String*/ title) {
		this.title = title;
		this.titlePaneWidget.attr('title', this.title);
	},
	
	_getCurrentItemAttr: function () {
		return this.filteringSelectWidget.attr('item');
	},
	
	_setCurrentItemAttr: function (/*Object*/ item) {
		// FIXME: handle this properly (lookup in datastore then call attr('value',...) )
		this.filteringSelectWidget.attr('value', item.id);
		//this._handleChange();
	},
	
	// --- public methods

	getMapStyleById: function (/*Number*/ id) {
		return this._mapStyles[id];
	},
	
	getCurrentMapStyle: function () {
		return this._mapStyles[this._currentId];
	},
	
	addMapStyle: function (/*String*/ label,
			/*GeoSOA.Spatialytics.mapstyles.MapStyle*/ mapStyle) {
		var id = this._mapStyleCounter;
		this.filteringSelectWidget.attr('store').newItem({id: id, label: label});
		this._mapStyles[id] = mapStyle;
		
		if(id == 0) {
			// first MapStyle added, set the current item in the FilteringSelect
			// with the added MapStyle
			this.filteringSelectWidget.attr('value', 0);
		}
		this._mapStyleCounter++;
	},
	
	attachSOLAPContext: function (/*GeoSOA.Spatialytics.SOLAPContext*/ solapContext) {
		this._solapContext = solapContext;
	},
	
	detachSOLAPContext: function () {
		if (this._solapContext != null) {
			this._solapContext = null;
		}
	}
	
});
